<?php
error_reporting(E_ALL^E_NOTICE);
define('USE_REDIS', true);

$phpself = $_SERVER['PHP_SELF'];
$redirect = $_SERVER['REDIRECT_URL'];

$phpselfDir = preg_replace("#/{2,}#", '/', rtrim(str_replace('\\', '/', dirname($phpself)), '/'));
$redirect = preg_replace("#/{2,}#", '/', rtrim(str_replace('\\', '/', $redirect), '/'));
$redirect = $redirect ? $redirect : '/';
$phpselfDir = $phpselfDir ? $phpselfDir : '/';

if(in_array($redirect, [
		preg_replace("#/{2,}#", '/',"$phpselfDir/favicon.ico")
	])) {
	echo file_get_contents('favicon.ico');
	die;
}

if($redirect != $phpselfDir) {
	header("HTTP/1.1 404 Not Found");
	die;
}

$query = $_REQUEST['q'];
$q = substr(trim($query), 0, 3);
$qdata = null;

$cacheFile = 'cache/' . urlencode($q). '.html';

if(!is_dir('cache')) {
    mkdir('cache');
}

if(file_exists($cacheFile)) {
    echo file_get_contents($cacheFile);
    die;
} else {
    $content = file_get_contents('wbfull.dat');
    $data = unserialize($content);
    $qdata = $data[$q];
}

?>

<?php if(!empty($qdata)): ?>
    <?php ob_start() ?>
<?php endif; ?>

<html>
    <head>
		<meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
        <?php if (empty($q)): ?>
            <title>五笔字典查询</title>
        <?php else: ?>
            <?php if (!empty($qdata)): ?>
                <title><?php echo $q ?> － 五笔字典查询</title>
            <?php else: ?>
                <title>没有找到“<?php echo $q ?>” － 五笔字典查询</title>
            <?php endif; ?>
        <?php endif; ?>
		<style>
			table tr {
				height:30px;
			}
			* {
				font-family: "微软雅黑", "Consolas", "Courier New", serif;
			}
		</style>
    </head>
    <body>
        <form action="" id="qform">
            <label>
                <input id="q" type="text" name="q"
					   oninput="doQuery(event)" onpaste="doQueryPaste(event)"
					   value="<?php echo $query ?>" placeholder="输入汉字查询" maxlength="1"/>
            </label>
            <input type="submit" value="查询" />
        </form>
        <?php if (!empty($qdata)): ?>
            <table>
                <tr>
                    <td>汉字：</td>
					<td><b><?php echo $q ?></b></td>
                </tr>
                <tr>
                    <td>拼音：</td>
                    <td><span style="color:red;"><?php echo $qdata['pingyin'] ?></span></td>
                </tr>
                <tr>
                    <td>五笔：</td>
					<td><span style="font-family:'Lucida Console', Consolas, 'Courier New',serif;color:mediumblue;font-weight: bold;"><?php echo strtoupper(implode('　', $qdata['codes'])) ?></span></td>
                </tr>
                <tr>
                    <td>图解：</td>
                    <td><img src="data:image/gif;base64,<?php echo base64_encode(file_get_contents('img/'.urlencode($q).'.gif')) ?>" alt="<?php echo $q ?>"></td>
                </tr>
            </table>
		<?php else: ?>
			<?php if(!empty($query)): ?>
				没有找到符合的内容
			<?php endif; ?>
        <?php endif; ?>
    </body>
	<script type="text/javascript">
		window.onload = function () {
			document.getElementById('q').focus();
			document.getElementById('q').select();
		};
		
		function doQueryPaste(e) {
			var q = document.getElementById('q');
			
			var pastedText = undefined;
			if (window.clipboardData && window.clipboardData.getData) { // IE
				pastedText = window.clipboardData.getData('Text');
			} else {
				pastedText = e.clipboardData.getData('Text');//e.clipboardData.getData('text/plain');
			}
			
			if(pastedText == '') {
				return;
			}
			
			q.value = pastedText;
			e.preventDefault();
			doQuery();
			return false;
		}
		
		function doQuery() {
			if(/[a-zA-Z0-9]/.test(q.value[0])) {
				return ;
			}
			var qform = document.getElementById('qform');
			qform.submit();
		}

	</script>
</html>

<?php if(!empty($qdata)): ?>
    <?php
        file_put_contents($cacheFile, ob_get_contents());
        ob_end_flush();
    ?>
<?php endif ?>
